package com.wave.wavesystem.ai.test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import com.wave.wavesystem.ai.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

public class FileOperationTool {

    private final String FILE_DIR = FileConstant.FILE_SAVE_DIR + "/file";


    @Tool(description = "read content from a file")
    public String readFile(@ToolParam(description = "name of a file") String fileName) {
        try {
            String filePath = FILE_DIR + "/" + fileName;
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "error read fail：" + e.getMessage();
        }
    }

    @Tool(description = "write content to a file")
    public String writeFile(@ToolParam(description = "name of a file") String fileName,
                           @ToolParam(description = "content of a file") String content) {
        String filePath =FILE_DIR + "/" + fileName;
        try {

            FileUtil.mkdir(FILE_DIR);

            FileUtil.writeUtf8String(content, filePath);
            return " write  to file  success to : " + filePath;

        } catch (IORuntimeException e) {
            return "error write  to file :  " + e.getMessage();
        }
    }

}
